package com.pan.admin.module.loanProduct.service.impl;

import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.module.loanProduct.dao.LoanProductPlanFieldDao;
import com.pan.admin.module.loanProduct.entity.FieldAndFieldValueVo;
import com.pan.admin.module.loanProduct.entity.LoanProductPlanField;
import com.pan.admin.module.loanProduct.entity.ProductFieldVo;
import com.pan.admin.module.loanProduct.entity.vo.ProductFieldAndFieldValueVo;
import com.pan.admin.module.loanProduct.service.LoanProductPlanFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品机构，资质字段匹配Service业务层处理
 *
 * @author ts
 * @date 2023-09-15
 */
@Service
public class LoanProductPlanFieldServiceImpl implements LoanProductPlanFieldService {

    @Autowired
    private LoanProductPlanFieldDao loanProductPlanFieldDao;

    /**
     * 根据ID获取列表产品机构，资质字段匹配
     *
     * @param id 产品机构，资质字段匹配主键
     * @return 产品机构，资质字段匹配
     */
    @Override
    public LoanProductPlanField getById(Long id) {
        return null;
    }

    /**
     * 查询产品机构，资质字段匹配列表
     *
     * @param loanProductPlanField 产品机构，资质字段匹配
     * @return 产品机构，资质字段匹配
     */
    @Override
    public List<LoanProductPlanField> list(LoanProductPlanField loanProductPlanField) {
        List<LoanProductPlanField> listIds = new ArrayList<>();
        List<LoanProductPlanField> list = loanProductPlanFieldDao.list(loanProductPlanField);

        if (!ValidParam.isEmpty(list)) {
            for (LoanProductPlanField obj : list) {
                LoanProductPlanField productFieldValueVo = new LoanProductPlanField();

                productFieldValueVo.setLoanFormFieldId(obj.getLoanFormFieldId());
                productFieldValueVo.setFieldValue(obj.getFieldValue());
                productFieldValueVo.setLoanFormFieldValueId(obj.getLoanFormFieldValueId());
                productFieldValueVo.setFieldName(obj.getFieldName());
                listIds.add(productFieldValueVo);
            }
        }
        return listIds;
    }

    /**
     * 查询所有字段和所属字段值列表
     */
    @Override
    public List<ProductFieldVo> listFieldAndValue(LoanProductPlanField field) {
        List<FieldAndFieldValueVo> list = loanProductPlanFieldDao.listFieldAndValue(field);
        FieldAndFieldValueVo fieldValueVo = new FieldAndFieldValueVo();
        List<ProductFieldVo> fieldValueVoList = fieldValueVo.getList(list);
        return fieldValueVoList;
    }

    /**
     * 批量删除产品机构，资质字段匹配
     *
     * @param productList 需要删除的产品机构，资质字段匹配主键
     * @return 结果
     */
    @Override
    @Transactional
    public int batchDeleteByListIds(List<ProductFieldAndFieldValueVo> productList) {
        int i = 1;

        if (!ValidParam.isEmpty(productList)) {
            i = loanProductPlanFieldDao.batchDeleteByListIds(productList);
        }
        return i;
    }

    /**
     * 新增机构计划资质计划
     *
     * @param loanProductPlanField 产品机构，资质字段匹配
     * @return 结果
     */
    @Override
    public int insertLoanProductPlanField(List<LoanProductPlanField> loanProductPlanField) {
        int i = 1;
        boolean judge = true;
        if (!ValidParam.isEmpty(loanProductPlanField)) {
            for (LoanProductPlanField obj : loanProductPlanField) {
                LoanProductPlanField byId = loanProductPlanFieldDao.getById(obj.getLoanProductId(), obj.getLoanFormFieldId(), obj.getLoanFormFieldValueId(), obj.getLoanProductPlanId());

                if (!ValidParam.isNull(byId)) {
                    judge = false;
                }
            }

            if (judge){
                i = loanProductPlanFieldDao.insertLoanProductPlanField(loanProductPlanField);
            }
        }
        return i;
    }

    /**
     * 修改产品机构，资质字段匹配
     *
     * @param loanProductPlanField 产品机构，资质字段匹配
     * @return 结果
     */
    @Override
    public int updateLoanProductPlanField(LoanProductPlanField loanProductPlanField) {
        return loanProductPlanFieldDao.updateLoanProductPlanField(loanProductPlanField);
    }

    /**
     * 批量删除产品机构，资质字段匹配
     *
     * @param ids 需要删除的产品机构，资质字段匹配主键
     * @return 结果
     */
    @Override
    public int batchDeleteById(Long[] ids) {
        return loanProductPlanFieldDao.batchDeleteById(ids);
    }


    /**
     * 删除产品机构，资质字段匹配信息
     *
     * @param id 产品机构，资质字段匹配主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id) {
        return loanProductPlanFieldDao.deleteById(id);
    }
}
